﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;

namespace smartphone.App_Code
{
    public class Order
    {
        /// <summary>
        /// ma don hang
        /// </summary>
        private int orderID;

        public int OrderID
        {
            get { return orderID; }
            set { orderID = value; }
        }
        /// <summary>
        /// tong so tieng
        /// </summary>
        private decimal totalAmount;

        public decimal TotalAmount
        {
            get { return totalAmount; }
            set { totalAmount = value; }
        }

        /// <summary>
        /// ngay tao don hang
        /// </summary>
        private DateTime dateCreated;

        public DateTime DateCreated
        {
            get { return dateCreated; }
            set { dateCreated = value; }
        }

        /// <summary>
        /// ngay giao hang
        /// </summary>
        private DateTime dateShipped;

        public DateTime DateShipped
        {
            get { return dateShipped; }
            set { dateShipped = value; }
        }

        /// <summary>
        /// don hang da xac nhan
        /// </summary>
        private bool verified;

        public bool Verified
        {
            get { return verified; }
            set { verified = value; }
        }

        /// <summary>
        /// don hang da giao (da hoan thanh)
        /// </summary>
        private bool completed;

        public bool Completed
        {
            get { return completed; }
            set { completed = value; }
        }

        /// <summary>
        /// don hang da huy
        /// </summary>
        private bool canceled;

        public bool Canceled
        {
            get { return canceled; }
            set { canceled = value; }
        }

        /// <summary>
        /// thong tin them ve don hang
        /// </summary>
        private string comments;

        public string Comments
        {
            get { return comments; }
            set { comments = value; }
        }

        /// <summary>
        /// ten khach hang
        /// </summary>
        private string customerName;

        public string CustomerName
        {
            get { return customerName; }
            set { customerName = value; }
        }

        /// <summary>
        /// dia chi giao hang
        /// </summary>
        private string shippingAddress;

        public string ShippingAddress
        {
            get { return shippingAddress; }
            set { shippingAddress = value; }
        }
        private string customerEmail;


        /// <summary>
        /// email khach hang
        /// </summary>
        public string CustomerEmail
        {
            get { return customerEmail; }
            set { customerEmail = value; }
        }


        /// <summary>
        /// lay thong tin don hang
        /// </summary>
        /// <param name="orderID">ma don hang muon lay thong tin</param>
        /// <returns></returns>
        public Order OrderInfo(string orderID)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "OrderGetInfo";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maDonHang";
            param.Value = orderID;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
            DataRow orderRow = table.Rows[0];
            //lay thong tin tra ve luu vao 1 doi tuong don hang
            Order orderInfo = new Order();
            orderInfo.OrderID = Int32.Parse(orderRow["OrderID"].ToString());
            orderInfo.TotalAmount = Decimal.Parse(orderRow["TotalAmount"].ToString());
            orderInfo.DateCreated = DateTime.ParseExact(orderRow["DateCreated"].ToString(), "yyyy-MM-dd", null);
            orderInfo.DateShipped = DateTime.ParseExact(orderRow["DateShipped"].ToString(), "yyyy-MM-dd", null);
            orderInfo.Verified = bool.Parse(orderRow["Verified"].ToString());
            orderInfo.Completed = bool.Parse(orderRow["Completed"].ToString());
            orderInfo.Canceled = bool.Parse(orderRow["Canceled"].ToString());
            orderInfo.Comments = orderRow["Comments"].ToString();
            orderInfo.CustomerName = orderRow["CustomerName"].ToString();
            orderInfo.ShippingAddress = orderRow["ShippingAddress"].ToString();
            orderInfo.CustomerEmail = orderRow["CustomerEmail"].ToString();

            return orderInfo;
        }

        /// <summary>
        /// Lay thong tin chi tiet cua 1 don hang, co bao gom thong tin ve san pham
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public DataTable GetDetails(string orderID)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "OrderGetDetails";
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maDonHang";
            param.Value = orderID;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
            return table;
        }
        
    
        /// <summary>
        /// cap nhat thong tin don hang
        /// </summary>
        /// <param name="orderInfo">Doi tuong don hang truyen vao de cap nhat</param>
        public static void Update(Order orderInfo)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "OrderUpdate";
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maDonHang";
            param.Value = orderInfo.OrderID;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@NgayTao";
            param.Value = orderInfo.DateCreated;
            param.DbType = DbType.DateTime;
            comm.Parameters.Add(param);

            if (orderInfo.DateShipped == null)
            {
            param = comm.CreateParameter();
            param.ParameterName = "@ngayGiao";
            param.Value = orderInfo.DateShipped;
            param.DbType = DbType.DateTime;
            comm.Parameters.Add(param);
            }
            
            param = comm.CreateParameter();
            param.ParameterName = "@daXacNhan";
            param.Value = orderInfo.Verified;
            param.DbType = DbType.Byte;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@daHoanThanh";
            param.Value = orderInfo.Completed;
            param.DbType = DbType.Byte;
            comm.Parameters.Add(param);
            
            param = comm.CreateParameter();
            param.ParameterName = "@daHuy";
            param.Value = orderInfo.Canceled;
            param.DbType = DbType.Byte;
            comm.Parameters.Add(param);
            
            param = comm.CreateParameter();
            param.ParameterName = "@thongTin";
            param.Value = orderInfo.Comments;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);
            
            param = comm.CreateParameter();
            param.ParameterName = "@tenKhachHang";
            param.Value = orderInfo.CustomerName;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);
            
            param = comm.CreateParameter();
            param.ParameterName = "@diaChiGiaoHang";
            param.Value = orderInfo.ShippingAddress;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);
            
            param = comm.CreateParameter();
            param.ParameterName = "@emailKhachHang";
            param.Value = orderInfo.CustomerEmail;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);
            
            GenericDataAccess.ExecuteNonQuery(comm);
        }

        /// <summary>
        /// Danh dau 1 don hang la dang duoc xac nhan
        /// </summary>
        /// <param name="orderId">ma don hang</param>
        public void MarkVerified(string orderId)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "OrderMarkVerified";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maDonHang";
            param.Value = orderId;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            GenericDataAccess.ExecuteNonQuery(comm);
        }

        /// <summary>
        /// set 1 don hang la da hoan thanh
        /// </summary>
        /// <param name="orderId"></param>
        public void MarkCompleted(string orderId)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "OrderMarkCompleted";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maDonHang";
            param.Value = orderId;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            // return the results
            GenericDataAccess.ExecuteNonQuery(comm);
        }

        /// <summary>
        /// Set 1 don hagn la da huy
        /// </summary>
        /// <param name="orderId"></param>
        public static void MarkCanceled(string orderId)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "OrderMarkCanceled";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maDonHang";
            param.Value = orderId;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            GenericDataAccess.ExecuteNonQuery(comm);
        }


        /// <summary>
        /// Lay comment cac don hang gan day nhat tinh tu so do nguoi dung nhap vao
        /// </summary>
        /// <param name="count">so ngay de xet</param>
        /// <returns></returns>
        public DataTable OrdersGetByRecent(int count)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "OrdersGetByRecent";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@soDem";
            param.Value = count;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            return GenericDataAccess.ExecuteSelectCommand(comm);
        }

        /// <summary>
        /// Lay cac don hang duoc tao trong khoang thoi gian do nguoi dung nhap vao
        /// </summary>
        /// <param name="startDate">ngay bat dau xet</param>
        /// <param name="endDate">ngay ket thuc</param>
        /// <returns></returns>
        public DataTable GetByDate(DateTime startDate, DateTime endDate)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "OrdersGetByDate";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@ngayDau";
            param.Value = startDate;
            param.DbType = DbType.Date;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@ngayKet";
            param.Value = endDate;
            param.DbType = DbType.Date;
            comm.Parameters.Add(param);

            return GenericDataAccess.ExecuteSelectCommand(comm);
        }


        /// <summary>
        /// Lay cac don hang cac duoc xac nhan hoac xoa
        /// </summary>
        /// <returns></returns>
        public DataTable GetUnverifiedUncanceled()
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "OrdersGetUnverifiedUncanceled";

            return GenericDataAccess.ExecuteSelectCommand(comm);
        }

        /// <summary>
        /// Lay cac don hang can duoc ship/hoan thanh
        /// </summary>
        /// <returns></returns>
        public DataTable GetVerifiedUncompleted()
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "OrdersGetVerifiedUncompleted";

            return GenericDataAccess.ExecuteSelectCommand(comm);
        }
    }

}